package crm.dao.impl;

import crm.Utils.DBUtils;
import crm.Utils.JDBCUtil;
import crm.dao.CustomerDao;
import crm.entity.Customer;

import java.util.*;

public class CustomerSqlSeverImpl extends JDBCUtil implements CustomerDao {
    //  注册用户  账号 密码
    @Override
    public int add(Customer customer) {
        String sql = "insert into customer (customer_name,customer_pwd) values(?,?)";
        Object[] params = {customer.getCustomer_name(), customer.getCustomer_pwd()};
        int i = this.executeUpdate(sql, params);
        return i;
    }

    //  注册用户基本信息
    @Override
    public int addAll(Customer customer) {
        String sql = "insert into customer (customer_name,customer_pwd,customer_phone,customer_address,credit,products,consume_time,contact_id,product_id,create_time,product_type,price) values(?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] params = {customer.getCustomer_name(), customer.getCustomer_pwd(), customer.getCustomer_phone(), customer.getCustomer_address(), customer.getCredit(), customer.getProducts(), customer.getConsume_time(), customer.getContact_id(),customer.getProduct_id(),customer.getCreate_time(),customer.getProduct_type(),customer.getPrice()};
        int i = this.executeUpdate(sql, params);
        return i;
    }

    @Override
    public List<Customer> getByName(String name) {
        String sql = "select * from customer where customer_name=?";
        List<Customer> productlist = new ArrayList<Customer>();

        List<Map<String, Object>> maplist = DBUtils.query(sql, name);
        for (Map<String, Object> map : maplist) {
            Customer customer = new Customer();
            customer.setCustomer_id((Integer) map.get("customer_id"));
            customer.setCustomer_name((String) map.get("customer_name"));
            customer.setCustomer_pwd((String) map.get("customer_pwd"));
            customer.setCustomer_phone((String) map.get("customer_phone"));
            customer.setCustomer_address((String) map.get("customer_address"));
            customer.setCredit((String) map.get("credit"));
            customer.setProducts((String) map.get("products"));
            customer.setContact_id((String) map.get("Contact_id"));
            customer.setProduct_id((String) map.get("product_id"));
            customer.setCreate_time((String) map.get("create_time"));
            customer.setProduct_type((String) map.get("product_type"));
            customer.setPrice((Integer) map.get("price"));
            customer.setConsume_time((Date) map.get("consume_time"));
            productlist.add(customer);
        }
        return productlist;
    }



    //
    @Override
    public boolean findCum(Customer customer) {
        Boolean flag = false;
        ArrayList<HashMap> list = new ArrayList<>();

        String sql = "select * from customer where customer_name =? and customer_pwd=?";
        Object[] params = {customer.getCustomer_name(), customer.getCustomer_pwd()};


        list = this.executeQuery(sql, params);
        if (list.size() > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean findAll(Customer customer) {
        Boolean flag = false;
        ArrayList<HashMap> list = new ArrayList<>();

        String sql = "select * from customer where customer_id=?";
        Object[] params = {customer.getCustomer_id()};

        list = this.executeQuery(sql, params);
        if (list.size() > 0) {
            flag = true;
        }
        return flag;
    }

    //    通过客户ID 删除
    @Override
    public int deleteByName(Customer customer) {
        String sql = "delete from customer where  customer_name=?";
        int update = DBUtils.update(sql, customer.getCustomer_name());
        return update;
    }

    @Override
    public int deleteByName(String name) {
        Customer customer = new Customer();
        String sql = "delete from customer where  customer_name=?";
        Object[] params = {customer.getCustomer_name()};
//        int update = DBUtils.update(sql, params);
//        return update;
        int i = this.executeUpdate(sql, params);
        return i;

    }

    @Override
    public int deleteById(int id) {
        Customer customer = new Customer();
        String sql = "delete from customer where  customer_id=?";
        Object[] params = {customer.getCustomer_id()};
        int i = this.executeUpdate(sql, params);
        return i;
    }

    //    修改客户信息
    @Override
    public int Update(Customer customer) {
        String sql = "UPDATE customer SET customer_pwd=?,customer_phone=?,customer_address=?,credit=?,products=?,consume_time=?,Contact_id=? WHERE customer_name=?";
        Object[] params = {customer.getCustomer_pwd(), customer.getCustomer_phone(), customer.getCustomer_address(), customer.getCredit(), customer.getProducts(), customer.getConsume_time(), customer.getContact_id(), customer.getCustomer_name()};
        int i = this.executeUpdate(sql, params);
        return i;
    }

    @Override
    public boolean updatePro(Customer customer) {
        Boolean flag = false;
        String sql = "UPDATE customer SET products=?,product_id=?,consume_time=?,create_time=?,product_type=?,price=? WHERE customer_name=?";
        Object[] params = {customer.getProducts(), customer.getProduct_id(), customer.getConsume_time(),
                customer.getCreate_time(), customer.getProduct_type(), customer.getPrice(),customer.getCustomer_name()};
        int i = this.executeUpdate(sql, params);
        if (i>0){
            flag =true;
        }
        return flag;
    }

    @Override
    public boolean findByName(Customer customer) {
        Boolean flag = false;
        ArrayList<HashMap> list = new ArrayList<>();

        String sql = "select * from customer where customer_name=?";
        Object[] params = {customer.getCustomer_name()};

        list = this.executeQuery(sql, params);
        if (list.size() > 0) {
            flag = true;
        }
        return flag;
    }




}







